﻿using System;
using System.Collections.Generic;
using System.Text;
using App1AppHandler;
using System.IO;
using System.Xml;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Equipment : Loot, ICloneable
    {
        public int Strength = 0;
        public int Intelligence = 0;
        public int Dexterity = 0;
        public int Agility = 0;
        public int Wisdom = 0;

        public int HP = 0;
        public int HP_Regen = 0;
        public int Stam = 0;
        public int Stam_Regen = 0;
        public int Mana = 0;
        public int Mana_Regen = 0;

        public string Slot = "";
        public string Material = "";
        public int Armor = 0;

        public int ResistFire = 0;
        public int ResistIce = 0;
        public int ResistHoly = 0;
        public int ResistUnholy = 0;
        public int ResistMagic = 0;
        public int ResistPhysical = 0;

        public int WaterMotion = -1;

        public List<LevelRequirement> RequirementsAll = new List<LevelRequirement>();
        public List<LevelRequirement> RequirementsAny = new List<LevelRequirement>();

        public Equipment(AppHandler2 appHandler) : base(appHandler){}

        protected void copyProperties(Equipment copyTo)
        {
            base.copyProperties(copyTo);

            copyTo.Strength = this.Strength;
            copyTo.Intelligence = this.Intelligence;
            copyTo.Dexterity = this.Dexterity;
            copyTo.Agility = this.Agility;
            copyTo.Wisdom = this.Wisdom;
            copyTo.HP = this.HP;
            copyTo.HP_Regen = this.HP_Regen;
            copyTo.Stam = this.Stam;
            copyTo.Stam_Regen = this.Stam_Regen;
            copyTo.Mana = this.Mana;
            copyTo.Mana_Regen = this.Mana_Regen;
            copyTo.Slot = this.Slot;
            copyTo.Material = this.Material;
            copyTo.Armor = this.Armor;
            copyTo.ResistFire = this.ResistFire;
            copyTo.ResistIce = this.ResistIce;
            copyTo.ResistHoly = this.ResistHoly;
            copyTo.ResistUnholy = this.ResistUnholy;
            copyTo.ResistMagic = this.ResistMagic;
            copyTo.ResistPhysical = this.ResistPhysical;

            copyTo.WaterMotion = this.WaterMotion;

            //This should be cloned... just using reference for now
            copyTo.RequirementsAll = this.RequirementsAll;
            copyTo.RequirementsAny = this.RequirementsAny;
        }

        public override string toXML(Character sendingToCharacter)
        {
            string result = "";

            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmldoc = new XmlTextWriter(stringWriter);

            xmldoc.WriteStartElement("i");
            //Loot Attributes
            xmldoc.WriteAttributeString("a", ID.ToString());
            xmldoc.WriteAttributeString("b", GameUID.ToString());
            xmldoc.WriteAttributeString("c", Name.ToString());
            xmldoc.WriteAttributeString("d", ClassName.ToString());
            xmldoc.WriteAttributeString("e", ItemType.ToString());
            xmldoc.WriteAttributeString("f", ImageURL.ToString());
            xmldoc.WriteAttributeString("g", CanConsume.ToString());
            xmldoc.WriteAttributeString("h", Bag.ToString());
            xmldoc.WriteAttributeString("i", Belt.ToString());
            xmldoc.WriteAttributeString("j", Equip.ToString());
            xmldoc.WriteAttributeString("k", Description.ToString());
            xmldoc.WriteAttributeString("l", PriceLow.ToString());
            xmldoc.WriteAttributeString("m", PriceHigh.ToString());

            xmldoc.WriteAttributeString("n", Weight.ToString());
            xmldoc.WriteAttributeString("o", BoundTo.ToString());
            xmldoc.WriteAttributeString("p", Lore.ToString());

            //Equipment Attributes
            xmldoc.WriteAttributeString("r", Strength.ToString());
            xmldoc.WriteAttributeString("s", Intelligence.ToString());
            xmldoc.WriteAttributeString("t", Dexterity.ToString());
            xmldoc.WriteAttributeString("u", Agility.ToString());
            xmldoc.WriteAttributeString("v", Wisdom.ToString());
            xmldoc.WriteAttributeString("w", Slot.ToString());

            xmldoc.WriteAttributeString("x", Armor.ToString());

            xmldoc.WriteAttributeString("y", HP.ToString());
            xmldoc.WriteAttributeString("z", HP_Regen.ToString());
            xmldoc.WriteAttributeString("A", Stam.ToString());
            xmldoc.WriteAttributeString("B", Stam_Regen.ToString());
            xmldoc.WriteAttributeString("C", Mana.ToString());
            xmldoc.WriteAttributeString("D", Mana_Regen.ToString());

            xmldoc.WriteAttributeString("E", ResistFire.ToString());
            xmldoc.WriteAttributeString("F", ResistIce.ToString());
            xmldoc.WriteAttributeString("G", ResistHoly.ToString());
            xmldoc.WriteAttributeString("H", ResistUnholy.ToString());
            xmldoc.WriteAttributeString("I", ResistMagic.ToString());

            xmldoc.WriteAttributeString("V", ResistPhysical.ToString());

            //Equipment Requirements
            xmldoc.WriteStartElement("J");
            foreach (LevelRequirement levelRequirement in RequirementsAll)
            {
                xmldoc.WriteRaw(levelRequirement.toXML());
            }
            xmldoc.WriteEndElement();

            xmldoc.WriteStartElement("K");
            foreach (LevelRequirement levelRequirement in RequirementsAny)
            {
                xmldoc.WriteRaw(levelRequirement.toXML());
            }
            xmldoc.WriteEndElement();

            xmldoc.WriteEndElement();
            xmldoc.Flush();

            xmldoc.Close();

            stringWriter.Flush();

            result = stringWriter.ToString();

            return result;
        }

        #region ICloneable Members

        public override object Clone()
        {
            Equipment clonedEquipment = new Equipment(Managers.GameHandler);
            copyProperties(clonedEquipment);

            return clonedEquipment;
        }

        #endregion
    }
}
